package com.fingard.dsp.bank.directbank.fngrd01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.ReqQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.RetAtsQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.detail.RetAtsRefundDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 融汇通退票查询
 */
public class FnGrd01QryRefund extends FnGrd01Base implements IQryRefund {
    @Override
    public RetAtsQryRefund d_qry_refund(ReqQryRefund p_req) throws Exception {
        RetAtsQryRefund retAts = new RetAtsQryRefund(p_req.reqHeader);
        retAts.writeStart();

        String tmpRespCode = "";
        String tmpRespInfo = "";
        
        try {
        	FnGrd01Header bankHeader = new FnGrd01Header();
            bankHeader.CommandCode = "22238";

            bankHeader.CorpBankCode = getBankConfig().getMapValueByKey("corBankCodeMapping", p_req.payAct);
            if (StringHelper.isNullOrEmpty(bankHeader.CorpBankCode)) {
                WriteBankLogLn2("CorpBankCode为空，请配置corBankCodeMapping映射");
            }

            bankHeader.TransSeqID = getQryTransID();
            bankHeader.SourceChannel = "ATS01";

            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, bankHeader);

            xmlWriter.writeStartElement("TransReq");
            xmlWriter.writeElementString("PayBank", p_req.payBank);
            xmlWriter.writeElementString("PayAct", p_req.payAct);
            xmlWriter.writeElementString("QueeStartDate", Format.DateTimeFormat.format(p_req.getQryStartDate()));
            xmlWriter.writeElementString("QueeEndDate", Format.DateTimeFormat.format(p_req.getQryEndDate()));
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/Root/Head/RespCode");
                    String tmpRetMsg = tmpBkReader.getFirstText("/Root/Head/RespInfo");

                    tmpRespCode = "0";

                    if ("0".equals(tmpRetCode)) {
                    	retAts.totleNum = tmpBkReader.getFirstText("/Root/TransResp/TotleNum");
                    	retAts.totleAmount = tmpBkReader.getFirstText("/Root/TransResp/TotleAmount");
                        
                        while (tmpBkReader.read("/Root/TransResp/DetailRecord")) {
                            RetAtsRefundDetail tmpRetDetail = new RetAtsRefundDetail();

                            tmpRetDetail.reqBatchNO = tmpBkReader.readedXElement.getSingleText("ReqSeqID");
                            tmpRetDetail.askNum = tmpBkReader.readedXElement.getSingleText("AskNum");
                            tmpRetDetail.strAbstract = tmpBkReader.readedXElement.getSingleText("StrAbstract");
                            tmpRetDetail.payAct = tmpBkReader.readedXElement.getSingleText("PayAct");
                            tmpRetDetail.payActName = tmpBkReader.readedXElement.getSingleText("PayActName");
                            tmpRetDetail.recAct = tmpBkReader.readedXElement.getSingleText("RecAct");
                            tmpRetDetail.recActName = tmpBkReader.readedXElement.getSingleText("RecActName");
                            tmpRetDetail.recBank = tmpBkReader.readedXElement.getSingleText("RecBank");
                            tmpRetDetail.amount = Format.DecimalAmtFormat.format(Double.parseDouble(tmpBkReader.readedXElement.getSingleText("Amount")));
                            tmpRetDetail.recAreaName = tmpBkReader.readedXElement.getSingleText("RecAreaName");
                            tmpRetDetail.memo = tmpBkReader.readedXElement.getSingleText("Memo");
                            tmpRetDetail.refundTicketDate = Format.Date10Format.format(Format.Date10Format.parse(tmpBkReader.readedXElement.getSingleText("RefundTicketDate")));
                            tmpRetDetail.refundTicketReason = tmpBkReader.readedXElement.getSingleText("RefundTicketReason");
                            tmpRetDetail.submitTime = Format.Date10Format.format(Format.Date10Format.parse(tmpBkReader.readedXElement.getSingleText("RefundTicketDate")));

                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpRetMsg;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRespCode = "-1";
            tmpRespInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

       if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        }

        retAts.writeEnd();
        return retAts;
    }
}
